﻿using CarRental.DDL;
using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace CarRental.View.Car {
    public partial class EditCar : System.Web.UI.Page {
        DbAccess __dbAccess = new DbAccess();
        car __car = new car();
        string __carId = "";
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                __carId = Request.QueryString["CarId"];

                ddlDepot.DataSource = new CarRental.Helpers.OptionItems.DepotLst(false);
                ddlDepot.DataValueField = "Key";
                ddlDepot.DataTextField = "Title";
                ddlDepot.DataBind();

                ddlDrivetrains.DataSource = new CarRental.Helpers.OptionItems.DriveTrainsLst(false);
                ddlDrivetrains.DataValueField = "Key";
                ddlDrivetrains.DataTextField = "Title";
                ddlDrivetrains.DataBind();

                ddlColor.DataSource = new CarRental.Helpers.OptionItems.ColorLst(false);
                ddlColor.DataValueField = "Key";
                ddlColor.DataTextField = "Title";
                ddlColor.DataBind();

                ddlCarType.DataSource = new CarRental.Helpers.OptionItems.CarTypeLst(false);
                ddlCarType.DataValueField = "Key";
                ddlCarType.DataTextField = "Title";
                ddlCarType.DataBind();

                ddlShop.DataSource = new CarRental.Helpers.OptionItems.ShopLst(false);
                ddlShop.DataValueField = "Key";
                ddlShop.DataTextField = "Title";
                ddlShop.DataBind();


                __car = __dbAccess.GetCar().Where(w => w.car_no == __carId).FirstOrDefault();
                if (__car != null) {
                    InitialRecord();
                }
                

            }
        }

        private void InitialRecord() {
            tbCarNo.Text = __car.car_no;
            ddlCarType.SelectedValue = __car.car_typecode;
            tbCarModel.Text = __car.car_model;
            ddlDepot.SelectedValue = __car.depot;
            ddlDrivetrains.SelectedValue = __car.drivetrains;
            ddlColor.SelectedValue = __car.color;
            tbTonnage.Text = __car.tonnage.ToString();
            tbBuyInDate.Text = __car.buy_in_date.ToString("yyyy-MM-dd");
            tbBuyinCost.Text = __car.buy_in_cost.ToString();
            tbRentPrice.Text = __car.rent_price.ToString();
            tbLastRentOrderNo.Text = __car.last_rental_order_no;
            tbLicense.Text = __car.license_no;
            ddlShop.SelectedValue = __car.shopcode;

        }

        protected void btnEditCar_Click(object sender, EventArgs e) {
            car _car = new car();
            _car = __dbAccess.GetCar().Where(w => w.car_no.ToUpper() == tbCarNo.Text.ToUpper()).FirstOrDefault();
            _car.rent_price = decimal.Parse(tbRentPrice.Text);
            _car.shopcode = ddlShop.SelectedValue;
            //_car.photo = ""; //for fai

            __dbAccess.UpdateCar(_car);
            MessageBox.Show("Update car success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Response.Redirect(ResolveUrl("~/View/Car/SearchCar.aspx"));
        }

        protected void btnDetele_Click(object sender, EventArgs e) {
            car _car = new car();
            _car = __dbAccess.GetCar().Where(w => w.car_no.ToUpper() == tbCarNo.Text.ToUpper()).FirstOrDefault();

            __dbAccess.DeleteCar(_car);
            MessageBox.Show("Delete car success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Response.Redirect(ResolveUrl("~/View/Car/SearchCar.aspx"));
        }

        protected void btnBack_Click(object sender, EventArgs e) {
            Response.Redirect(ResolveUrl("~/View/Car/SearchCar.aspx"));
        }
    }
}